package com.cloud.wms.ui.inside.transfer;

import android.app.Activity;
import android.content.Intent;
import android.os.Message;

import com.cloud.widget.ScanText;
import com.cloud.wms.R;
import com.cloud.wms.app.FineExApplication;
import com.cloud.wms.base.BaseBarActivity;
import com.cloud.wms.config.IntentKey;
import com.cloud.wms.config.SpKey;
import com.cloud.wms.ui.inside.contract.TransferDownContact;
import com.cloud.wms.ui.inside.entity.TransferCommodityEntity;
import com.cloud.wms.ui.inside.presenter.TransferDownPresenter;

import java.util.ArrayList;

import butterknife.BindView;

/**
 * Created by FC on 2021/5/17.
 * Desc：移库下架扫描商品
 */
public class TransferDownGoodsActivity extends BaseBarActivity<TransferDownPresenter> implements TransferDownContact.View {
    @BindView(R.id.scan_text)
    ScanText scanText;
    private String posID;

    @Override
    protected String barTitle() {
        return "移库下架";
    }

    @Override
    protected int getResId() {
        return R.layout.activity_transfer_down_start;
    }

    @Override
    protected void initEvent() {
        initScanText(scanText);
        scanText.setHint("商品条码");
        posID = getIntent().getStringExtra(IntentKey.ID_KEY);
    }

    @Override
    protected void onScanHandle(String result, int resultCode) {
        super.onScanHandle(result, resultCode);
        scanText.addHistory(result);
        mPresenter.getCommodityMsg(posID, result);
    }

    @Override
    public void onSuccess(Message message) {
        if (message.what == TransferDownPresenter.GET_COMMODITY_MSG) {
            ArrayList<TransferCommodityEntity> entityList = (ArrayList<TransferCommodityEntity>) message.obj;
            //是否需要扫描容器
            if (FineExApplication.component().kv().decodeBool(SpKey.TRANSFER_SCAN_CONTAINER, false)) {
                TransferDownContainerActivity.jumpToNext(this, entityList);
                return;
            }
            if (entityList.size() == 1)
                TransferDownConfirmActivity.jumpToNext(this, entityList.get(0), null);
            else
                TransChooseBatchActivity.jumpToNext(this, entityList, true, null);
        }
    }

    public static void jumpToNext(Activity activity, String posID) {
        Intent intent = new Intent(activity, TransferDownGoodsActivity.class);
        intent.putExtra(IntentKey.ID_KEY, posID);
        activity.startActivity(intent);
    }

    @Override
    protected void setupComponent() {
        getActivityComponent().inject(this);
    }
}
